/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fa.supporters;

import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author tatung
 */
public class AddTransactionTableModel extends AbstractTableModel {
  // Implementation of TableModel interface
    public int getRowCount() {
        return data.length;
    }

    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    public Object getValueAt(int row, int column) {
        return data[row][column];
    }

    public Class getColumnClass(int column) {
        return (data[0][column]).getClass();
    }

    public String getColumnName(int column) {
        return columnNames[column];
    }

    public boolean isCellEditable(int row, int column) {
        //return column == 1;
        return true;
    }

    public void setValueAt(Object value, int row, int column) {
        //if (isValidValue(value)) {
        {
          data[row][column] = value;
          fireTableRowsUpdated(row, row);
        }
    }

    public void addRow(Object[] value){
        Object[][] tmpData;
        if(data.length > 0){
            tmpData = new Object[data.length + 1][COLUMN_COUNT];
        } else{
            tmpData = new Object[1][COLUMN_COUNT];
        }
        for(int i=0;i<data.length;i++){
            for(int j=0;j<data[0].length;j++){
                tmpData[i][j] = data[i][j];
            }
        }
        for(int i=0;i<COLUMN_COUNT;i++){
            tmpData[tmpData.length - 1][i] = value[i];
        }
        data = tmpData;        
        fireTableRowsInserted(data.length - 1, data.length - 1);
    }

    public void deleteRow(int row){
        for(int i=row;i<data.length - 1;i++){
            for(int j=0;j<data[0].length;j++){
                data[i][j] = data[i+1][j];
            }
        }
        Object[][] tmpData = new Object[data.length - 1][data[0].length];
        for(int i=0;i<tmpData.length;i++){
            for(int j=0;j<tmpData[0].length;j++){
                tmpData[i][j] = data[i][j];
            }
        }
        data = tmpData;
        fireTableRowsDeleted(row, row);
    }

    // Extra public methods
//    public static String[] getValidStates() {
//        return validStates;
//    }

    // Protected methods
//    protected boolean isValidValue(Object value) {
//        if (value instanceof String) {
//            String sValue = (String)value;
//
//            for (int i = 0; i < validStates.length; i++) {
//                if (sValue.equals(validStates[i])) {
//                    return true;
//                }
//            }
//        }
//
//        return false;
//    }

    public Object[][] getData(){
        return data;
    }

    protected static final int COLUMN_COUNT = 4;

    protected static final String[] validStates = {
//        "On order", "In stock", "Out of print"
    };

    protected Object[][] data = new Object[][] {
        { "Chọn mặt hàng", "Số lượng", "Đơn vị", "Ghi chú"}
    };

    protected static final String[] columnNames = {
        "Mặt hàng", "Số lượng", "Đơn vị", "Ghi chú"
    };
}
